<?PHP



import('iforum.dao.mysql.MySQLDAO');

import('iforum.common.Util');

import('iforum.web.iForumWebAppContext');



/**

 * @package iforum.dao.mysql 

 * @author   iCer <iCer@justdn.com>

 * @version  $Id: MySQLPostDAO.php,v 1.3 2005/04/06 04:04:56 icer Exp $

 */

class MySQLPostDAO extends MySQLDAO  

{

 	var $context;

	var $registry;

	var $request;

	var $registryValues;



	function MySQLPostDAO()

	{

		$this->__constract();

	}



	function __constract()

	{
		parent::MySQLDAO();
		$this->context = &Singleton::instance('iForumWebAppContext');

		$this->registry = &$this->context->getRegistry();

		$this->request  = &$this->context->getRequest();

		$this->registryValues = &$this->registry->getRegistryValues();

	}	

	

	 /**

	  * @param  array $post 

	  * 	����������ݵĹ�j����

	  * 	example: $post = array('pGuid'=>'XXXX-...', 'tID'=>1, 'pTitle'=>'iCer');	

	  * @param string $postTable ���ӱ���,��'if_post1'

	  */

	 function addPost($post, $postTable)

	 {
		$insertSQL = getInsertSql($post, $postTable);

		$this->_dbAdapter->execute($insertSQL);
	 }	

	

	/**

	 * @param int $threadId 

	 * @param string $postTable  for more informatin,see addPost()

	 * @param int $numrows

	 * @param int $offset

	 * @return recordset

	 */

	 function &getPostsByThreadId($threadId, $postTable, $numrows=-1, $offset=-1)

	 {
	 	$sql = 'select * from '.$postTable.' where tID='.$threadId.' order by pID';

	 	return $this->_dbAdapter->selectLimit($sql, $numrows, $offset);

	 }

	 

	 /**

	  * @param array $threadIdList

	  * @return recordset 

	  */

	 function &getPostsByThreadIdList($threadIdList)

	 {

	 	

	 	

		if (is_array($threadIdList) && count($threadIdList)>0)

		{

			$list = implode($threadIdList,',');

				 	

	 		$sql = 'select * from '.$this->registryValues['data_table']['post_merge'].' where pGuid in ('.$list.')';

		}



		return $this->_dbAdapter->execute($sql);

	 }

	 

	 /**

	  * @param char   $postGuid

	  * @param array  $post for more informatin,see addPost()

	  * @param string $postTable for more informatin,see addPost()

	  */

	  function updatePost($postGuid, $post, $postTable)

	  {

			  	

	  	$updateSQL = getUpdateSql($post, $postTable, 'pGuid='.$postGuid);

		$this->_dbAdapter->execute($updateSQL);		  	

	  }

	 

	  /**

	   * @param string   $postGuid 

	   * @param string $postTable for more informatin,see addPost()

	   */

	  function deletePost($postGuid,$postTable)

	  {

	  	

	  	

		$insertSQL = 'insert into '.$this->registryValues['data_table']['post_recycle'].

				' select * from '.$postTable.' where pGuid='.$postGuid;

		$this->_dbAdapter->execute($insertSQL);	

				

	  	$deleteSQL = 'delete from '.$postTable.' where pGuid='.$postGuid;

	  	$this->_dbAdapter->execute($deleteSQL);		

	  }

	  

	  /**
	   * @param int    $threadId
	   * @param string $postTable for more informatin,see addPost()
	   * @return int   ��ɾ���������
	   */
	  function deletePostByThreadId($threadId, $postTable)

	  {
		$insertSQL = 'insert into '.$this->registryValues['data_table']['post_recycle'].
				' select * from '.$postTable.' where tID='.$threadId;
		$this->_dbAdapter->execute($insertSQL);

	  	$deleteSQL = 'delete from '.$postTable.' where tID='.$threadId;
	  	$this->_dbAdapter->execute($deleteSQL);

	  	return $this->_dbAdapter->affectedRows();	  	

	  }

	  

	  /**

	   * @param array  $postGuidList ������Guid������

	   * @param string $postTable    for more informatin,see addPost()

	   * @return int   ��ɾ���������

	   */

	  function deletePosts($postGuidList,$postTable)

	  {

	  	

	  	

		if (is_array($postGuidList) && count($postGuidList)>0)

		{

			$list = implode($postGuidList,',');

			

			$insertSQL = 'insert into '.$this->registryValues['data_table']['post_recycle'].

				' select * from '.$postTable.' where pGuid in ('.$list.')';

			$deleteSQL = 'delete from '.$postTable.' where pGuid in ('.$list.')';

		}

		$this->_dbAdapter->execute($insertSQL);

		$this->_dbAdapter->execute($deleteSQL);	  	

	  	return $this->_dbAdapter->affectedRows();

	  }



}



?>